var equipArr=[];
var sensorArr=[];
$(function() {
	$('#dataGrid').datagrid({
		url : 'monitor/dataGrid',
        fit: true,   //true,是让表格高度适应屏幕高度，为FALSE是适应内容
		striped : true,
		rownumbers : true,
		pagination : true,
		singleSelect : true,
		idField : 'id',
		sortName : 'id',
		sortOrder : 'asc',
		pageSize : 50,
		pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
		frozenColumns : [ [ {
			width : '110',
			title : '监控编号',
			field : 'monitorNum'
		},{
			width : '140',
			title : '设备名称',
			field : 'equipmentName'
		},{
			width : '140',
			title : '通道名称',
			field : 'channelName'
		},{
			width : '140',
			title : '传感器名称',
			field : 'sensorName'
		},{
			width : '160',
			title : '备注',
			field : 'descrip'
		},{
			field : 'action',
			title : '操作',
			width : 200,
			formatter : function(value, row, index) {
				var str = '&nbsp;';
				if(row.isdefault!=0){
//					if ($.canEdit) {							
						str += $.formatString('<a href="javascript:void(0)" onclick="editMonitor(\'{0}\');" >编辑</a>', index);
//					}
//					if ($.canDelete) {
						str += '&nbsp;&nbsp;|&nbsp;&nbsp;';
						str += $.formatString('<a href="javascript:void(0)" onclick="deleteMonitor(\'{0}\');" >删除</a>', row.id);
//					}
				}
				return str;
			}
		} ] ],
		toolbar : '#toolbar'
	});
	
	$("#dia-add").dialog({
		title:"添加监控传感器",
        modal: true,
        width: 500,  
        height: 350,
        closed: true,
        buttons : [{
			text : '添加',
			iconCls : 'icon-add',
			handler : function () {
				var bool=$("#form-add").form('validate');
				if(bool){
					$.ajax({
						cache:false,
						url:'monitor/add',
						data:$("#form-add").serialize(),
						type:"POST",
						dataType:"json",
						success:function(result){
							if(!result.success){
								alert(result.msg);
							}else{
								$("#dataGrid").datagrid('reload');
								$("#dia-add").dialog('close');
							}
						}
					});
				}
			}
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$("#dia-add").dialog('close');
			}
		}]
	});
	
	$("#dia-edit").dialog({
		title:"编辑监控传感器",
        modal: true,
        width: 550,  
        height: 300,
        closed: true,
        buttons : [{
			text : '编辑',
			iconCls : 'icon-edit',
			handler : function () {
				var bool=$("#form-edit").form('validate');
				if(bool){
					$.ajax({
						cache:false,
						url:'monitor/edit',
						data:$("#form-edit").serialize(),
						type:"POST",
						dataType:"json",
						success:function(map){
							if(!map.success){
								alert(map.msg);
							}else{
								$('#dataGrid').datagrid('reload');
								$("#dia-edit").dialog('close');
							}
						}
					});
				}
			}
		},{
			text : '取消',
			iconCls : 'icon-redo',
			handler : function () {
				$("#dia-edit").dialog('close');
			}
		}]
	});
	
	$.ajax({
		cache:false,
		url:'equipment/tree',
		type:"POST",
		dataType:"json",
		success:function(result){
			$.each(result,function(n,value) {
				equipArr.push({'value':value.id,'text':value.text});
			});
		}
	});
	
	$.ajax({
		cache:false,
		url:'sensor/getAllSensor',
		type:"POST",
		dataType:"json",
		success:function(result){
			$.each(result,function(n,value) {
				sensorArr.push({'value':value.value,'text':value.text});
			});
		}
	});
});

function addMonitor() {
	initEquip('#dia-add');
	initChannel('#dia-add #c_id');
	initSensor('#dia-add #s_id');
	$("#dia-add").dialog('open');
}


function editMonitor(index) {
	var rows=$('#dataGrid').datagrid('getRows');
	var row=rows[index];
	$("#dia-edit input[name=id]").val(row.id);
	$("#dia-edit input[name=monitorNum]").val(row.monitorNum);
	$("#dia-edit input[name=descrip]").val(row.descrip);
	initEquip('#dia-edit');
	initChannel('#dia-edit #c_id');
	initSensor('#dia-edit #s_id');
	$('#dia-edit #e_id').combobox('select',row.equipmentId);
	$('#dia-edit #c_id').combobox('select',row.channelId);
	$('#dia-edit #s_id').combobox('select',row.sensorId);
	$("#dia-edit").dialog('open');
	
}	

function deleteMonitor(id) {
	if(confirm("确定要删除当前传感器监测吗？")){
		$.ajax({
			cache:false,
			url:"monitor/delete",
			data:{id:id},
			type:"POST",
			dataType:"json",
			success:function(result) {
				if(result.success){
					$('#dataGrid').datagrid('reload');
				}else{
					alert(result.message);
				}
			}
		});
	}
}

function initEquip(id){
	$(id+' #e_id').combobox({
       valueField:'value',
       textField:'text',
       panelHeight:'auto',
       editable:false,
       data:equipArr,
       onChange: function (newValue, oldValue){
	       $.ajax({
	   			cache:false,
	   			url:"channel/getChannelByEquip",
	   			data:{equipmentId:newValue},
	   			type:"POST",
	   			dataType:"json",
	   			success:function(result) {
	   				console.log(result);
	   				$(id+' #c_id').combobox("loadData",result);
	   				$(id+' #c_id').combobox("unselect");
	   				if(result.length>0){
	   					$(id+' #c_id').combobox("select",result[0].value);
	   				}
	   			}
	   		});
       }
   });
}

function initChannel(id){
	$(id).combobox({
       valueField:'value',
       textField:'text',
       panelHeight:'auto',
       editable:false
   });
}

function initSensor(id){
	$(id).combobox({
       valueField:'value',
       textField:'text',
       panelHeight:'auto',
       editable:false,
       data:sensorArr
   });
}
